<?php

namespace app\adminmall\controller;
use app\adminmall\model\HelpsModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 帮助中心
 */
class HelpsController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new HelpsModel();
    }
    /**
     * 帮助中心列表
     * @author [lx] 
     */
    public function index( $page = 1){
        $list = Db::name('mall_helps')->alias('a')
        ->join('mall_helptype b','a.pid=b.id','LEFT')
        ->field('a.*,b.name')
        ->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     *添加
     * @author [lx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$param['add_time']=now_time();
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('Helps/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('Helps/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    
        /**
     * 帮助分类列表
     * @author [lx] 
     */
    public function indextype(){
	    $store_menu_list       = Db::name('mall_helptype') ->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
        $store_menu_level_list = array2level($store_menu_list);
        return $this->fetch('indextype',['store_menu_level_list'=>$store_menu_level_list]);
    }
    
    
    /**
     * 
     *添加分类
     * @author [lx] 
     */
    public function addtype()
    {
    	
    	$list = Db::name('mall_helptype')->where('pid',0)->select();
        if(input('title')){
        	$name=input('title');
			$param['name']=$name;
			$param['add_time']=now_time();
			$param['sort']=input('sort');
			$param['pid']=input('pid');
			if (Db::name('mall_helptype')->insert($param)!== false) {
                  $this->success('添加成功', url('Helps/indextype'));
          } else {
              $this->error('添加失败');
          }
        }
        return $this->fetch('addtype',['list'=>$list]);
    }

    /**
     * [del删除分类]
     * @author [lx] 
     */
    public function deletetype()
    {
        $id = input('param.id');
        if (Db::name('mall_helptype')->where('id',$id)->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
   
   
   
      /**
     * [uptate编辑分类]
     * @author [lx] 
     */ 
     
     
     public function updatetype(){
     	$id = input('param.id');
     	$ad=Db::name('mall_helptype')->find($id);
     	$list = Db::name('mall_helptype')->where('pid',0)->select();
     	$this->assign('ad',$ad);	
     	$this->assign('list',$list);
     	if(input('title')){
     	   $param['pid']=input('pid');
           $param['name']=input('title'); 
           $param['sort']=input('sort');
           $id=input('id');
		  if( Db::name('mall_helptype')->where('id', $id)->update($param)){
		  		$this->success('修改成功', url('Helps/indextype'));
          } else {
              $this->error('修改失败');
		  }
        }
      
        return $this->fetch();
     	
     }
}